package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.JobInfo;
import com.test.entity.NoticeInfo;
import com.test.service.DepartInfoService;
import com.test.service.NoticeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 公告(NoticeInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:51
 */
@Controller
public class AdminNoticeInfoController {


    @Autowired
    private NoticeInfoService noticeInfoService;


    //    显示页面信息
    @GetMapping("admin/notice_info")
    public String getCwInfo() {
        return "admin/notice_info/list";
    }

    //    显示公告管理信息
    @GetMapping("admin/notice_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page, NoticeInfo noticeInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(noticeInfoService.queryByPage(noticeInfo, PageRequest.of(page - 1, 10)));
    }

    //    跳转公告新增页面
    @GetMapping("admin/notice_info/add")
    public ModelAndView getAdd() {
        return new ModelAndView("admin/notice_info/add_page");
    }

    //    提交公告新增
    @PostMapping("admin/notice_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(NoticeInfo noticeInfo) {
        ResData resData = new ResData();
        int insert = noticeInfoService.insert(noticeInfo);
        if (insert>0) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    跳转职位修改页面
    @GetMapping("admin/notice_info/update")
    public ModelAndView getUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/notice_info/update_page");
        NoticeInfo noticeInfo = noticeInfoService.queryById(id);
        modelAndView.addObject("data", noticeInfo);
        return modelAndView;
    }

    //    提交职位修改页面信息
    @PostMapping("admin/notice_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id,NoticeInfo noticeInfo) {
        noticeInfo.setId(id);
        ResData resData = new ResData();
        NoticeInfo update = noticeInfoService.update(noticeInfo);
        if (update.getId() != null || !update.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }

    //    删除财务页面信息
    @GetMapping("admin/notice_info/del")
    @ResponseBody
    public ResData getDelete(Integer id) {
        ResData resData = new ResData();
        boolean b = noticeInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }


}

